/*
 * Copyright (c) 2025 Huawei Technologies Co., Ltd.
 * openUBMC is licensed under Mulan PSL v2.
 * You can use this software according to the terms and conditions of the Mulan PSL v2.
 * You may obtain a copy of Mulan PSL v2 at:
 *         http://license.coscl.org.cn/MulanPSL2
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
 * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
 * See the Mulan PSL v2 for more details.
 */

#include "mock_bus.h"

#include <mc/exception.h>

namespace test_utils {

uint32_t mock_bus::read(ACCESS_DATA_S& input) {
    ++read_call_count;
    (void)input;
    return HAS_OK;
}

uint32_t mock_bus::write(ACCESS_DATA_S& input) {
    block_write_calls.push_back({input.offset, input.in_buffer, input.out_buffer});

    if (throw_on_block_write) {
        throw std::runtime_error("mock_bus forced exception");
    }

    if (block_write_result != HAS_OK) {
        MC_THROW(mc::invalid_op_exception, "mock_bus write failed: ${error}",
                 ("error", block_write_error.empty() ? "unknown error" : block_write_error));
    }

    return block_write_result;
}

uint8_t mock_bus::get_id() {
    return m_id;
}

void mock_bus::bus_lock() {
    ++lock_call_count;
}

void mock_bus::bus_unlock() {
    ++unlock_call_count;
}

void mock_bus::set_id(uint8_t id) {
    m_id = id;
}

std::shared_ptr<mock_bus> mock_bus::shared_from_this() {
    return std::enable_shared_from_this<mock_bus>::shared_from_this();
}

} // namespace test_utils
